package cn.iocoder.yudao.module.bpm.service.oaleavapplysub;

import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oaapply.OaApplyDO;
import cn.iocoder.yudao.module.bpm.dal.mysql.oaapply.OaApplyMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.bpm.controller.admin.oaleavapplysub.vo.*;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oaleavapplysub.OaLeavApplySubDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.bpm.dal.mysql.oaleavapplysub.OaLeavApplySubMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.*;

/**
 * 请假申请 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class OaLeavApplySubServiceImpl extends ServiceImpl<OaLeavApplySubMapper, OaLeavApplySubDO> implements OaLeavApplySubService {

    @Resource
    private OaLeavApplySubMapper oaLeavApplySubMapper;

    @Override
    public Long createOaLeavApplySub(OaLeavApplySubSaveReqVO createReqVO) {
        // 插入
        OaLeavApplySubDO oaLeavApplySub = BeanUtils.toBean(createReqVO, OaLeavApplySubDO.class);
        oaLeavApplySubMapper.insert(oaLeavApplySub);
        // 返回
        return oaLeavApplySub.getId();
    }

    @Override
    public void updateOaLeavApplySub(OaLeavApplySubSaveReqVO updateReqVO) {
        // 校验存在
        validateOaLeavApplySubExists(updateReqVO.getId());
        // 更新
        OaLeavApplySubDO updateObj = BeanUtils.toBean(updateReqVO, OaLeavApplySubDO.class);
        oaLeavApplySubMapper.updateById(updateObj);
    }

    @Override
    public void deleteOaLeavApplySub(Long id) {
        // 校验存在
        validateOaLeavApplySubExists(id);
        // 删除
        oaLeavApplySubMapper.deleteById(id);
    }

    private void validateOaLeavApplySubExists(Long id) {
        if (oaLeavApplySubMapper.selectById(id) == null) {
             throw new ServiceException(500, "请假申请不存在");
        }
    }

    @Override
    public OaLeavApplySubDO getOaLeavApplySub(Long id) {
        return oaLeavApplySubMapper.selectById(id);
    }

    @Override
    public PageResult<OaLeavApplySubDO> getOaLeavApplySubPage(OaLeavApplySubPageReqVO pageReqVO) {
        return oaLeavApplySubMapper.selectPage(pageReqVO);
    }

}
